<?php

namespace app\shop\model\plus\agent;

use app\api\model\plus\agent\Referee as RefereeModel;
use app\common\model\plus\agent\Apply as ApplyModel;
use app\common\service\message\MessageService;
use app\common\model\user\User as UserModel;
use app\shop\model\store\Store;
use app\supplier\model\supplier\Supplier as SupplierModel;

/**
 * 分销商入驻申请模型
 */
class Apply extends ApplyModel
{
    /**
     * 获取分销商申请列表
     * @noinspection PhpUndefinedMethodInspection
     */
    public function getList($search)
    {
        $model = $this->alias('apply')
            ->field('apply.*, user.nickName, user.avatarUrl,user.god_supplier_id')
            ->with(['referee'])
            ->join('user', 'user.user_id = apply.user_id')
            ->order(['apply_status' => 'asc','apply.create_time' => 'desc']);
        if (!empty($search['nick_name'])) {
            $model = $model->where('user.nickName|apply.real_name|apply.mobile', 'like', '%' . $search['nick_name'] . '%');
        }

        // 获取列表数据
        $list = $model->paginate($search['list_rows']);
        foreach ($list as $key => $value){
            $god_supplier_array = json_decode($value['god_supplier_id'],true);
            $list[$key]['shop_supplier_name'] = (new SupplierModel())->where('shop_supplier_id','in',$god_supplier_array) -> column('name');
        }
        return $list;
    }

    /**
     * 分销商入驻审核
     * @param $data
     * @return bool
     */
    public function submit($data)
    {
        if ($data['apply_status'] == '30' && empty($data['reject_reason'])) {
            $this->error = '请填写驳回原因';
            return false;
        }
        $god_supplier_id_json = '';
        $update_data = [];
        if(isset($data['shop_supplier_id']) && $data['shop_supplier_id']){
            $god_supplier_id_json = json_encode($data['shop_supplier_id']);
            $update_data['god_supplier_id'] = $god_supplier_id_json;
        }

        $this->startTrans();
        try {
            if ($data['apply_status'] == '20') {
                // // 新增分销商用户
                // User::add($data['user_id'], [
                //     'real_name' => $data['real_name'],
                //     'mobile' => $data['mobile'],
                //     'referee_id' => $data['referee_id'],
                //     'grade_id' => (new Grade())->getDefaultGradeId()
                // ]);
                //$UserIsG = UserModel::where('user_id='.$data['user_id'])->value("is_golden");
                if($data['status'] == 0){
                    $is_golden = 0;
                }else{
                    $is_golden = 1;
                }
                $update_data['is_golden'] = $is_golden;
//                if($UserIsG==0){ //兼容未选择区域的数据操作 (kevin 2024/5/9)
                    UserModel::where('user_id='.$data['user_id'])->update($update_data);
//                }
            }
            $save_data = [
                'audit_time' => time(),
                'apply_status' => $data['apply_status'],
                'status' => $data['status'],
                'reject_reason' => $data['reject_reason'],
            ];
            $this->save($save_data);
            // // 记录推荐人关系
            // if ($data['referee_id'] > 0) {
            //     RefereeModel::createRelation($data['user_id'], $data['referee_id']);
            // }
            // // 发送模板消息
            // (new MessageService)->agent($this);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }

    /**
     * 获取申请中的数量
     */
    public static function getApplyCount()
    {
        return (new static())->where('apply_status', '=', 10)->count();
    }
}